package me.eachann.bar;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import me.eachann.library.R;

final class ViewBuilder {
    static RelativeLayout newMainLayout(Context context) {
        RelativeLayout mainLayout = new RelativeLayout(context);
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1));

        return mainLayout;
    }

    static View newLineView(Context context) {
        View lineView = new View(context);
        lineView.setBackgroundColor(Color.parseColor("#dcdcdc"));
        final float scale = context.getResources().getDisplayMetrics().density;
        lineView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (1 * scale + 0.5f)));
        return lineView;
    }

    static View newStatusBarView(Context context, int height) {
        View statusBarView = new View(context);
        statusBarView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
        return statusBarView;
    }

    static View newLeftView(Context context) {
        final float scale = context.getResources().getDisplayMetrics().density;
        ImageView back = new ImageView(context);
        RelativeLayout.LayoutParams backParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        backParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        back.setLayoutParams(backParams);
        back.setBackgroundResource(R.drawable.ic_back);
        RelativeLayout leftView = new RelativeLayout(context);
        RelativeLayout.LayoutParams startParams = new RelativeLayout.LayoutParams((int) (44 * scale + 0.5f), (int) (44 * scale + 0.5f));
        startParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        startParams.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE);
        leftView.setLayoutParams(startParams);
        leftView.addView(back);
        return leftView;
    }

    static TextView newTitleView(Context context) {
        final float scale = context.getResources().getDisplayMetrics().density;
        TextView titleView = new TextView(context);
        RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
//        titleParams.bottomMargin = (int) (11.5 * scale + 0.5f);
        titleView.setLayoutParams(titleParams);
        titleView.setGravity(Gravity.CENTER);
        titleView.setTextColor(Color.parseColor("#ff333333"));
        titleView.setTextSize(17);
//        titleView.setTypeface(Typeface.DEFAULT_BOLD);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            titleView.getPaint().setLetterSpacing(0.05f);
        }
        titleView.setSingleLine();
        titleView.setEllipsize(TextUtils.TruncateAt.END);
        return titleView;
    }

}
